

package com.zmks.jkgj.xuexiao.controller;




import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.zmks.jkgj.core.controller.ABaseController;
import com.zmks.jkgj.core.resp.DataResp;
import com.zmks.jkgj.core.resp.Resp;
import com.zmks.jkgj.core.util.IdcardValidator;
import com.zmks.jkgj.xuexiao.Session;
import com.zmks.jkgj.xuexiao.resp.WebResp;
import com.zmks.jkgj.xuexiao.service.ICrctObjAccountService;
import com.zmks.jkgj.xuexiao.service.ICrctObjTeacherService;
import com.zmks.jkgj.xuexiao.service.ICrctRelClassTeacherService;
import com.zmks.jkgj.xuexiao.tools.Md5;

import com.zmks.jkgj.xuexiao.vo.CrctObjAccountVO;
import com.zmks.jkgj.xuexiao.vo.CrctObjTeacherVO;
@Controller
public class CrctObjAccountController extends ABaseController {
	 @Autowired
		
		private ICrctObjAccountService crctObjAccountService;
	 @Autowired
		
		private ICrctObjTeacherService crctObjTeacherService;	
	
		 //查询
		@RequestMapping("/get_CrctObjAccount_list")
		@ResponseBody
		public Resp get_CrctObjAccount_list( Integer pageIndex,
				Integer pageSize,HttpServletRequest request) {
			WebResp 	webResp = new WebResp();
		   CrctObjAccountVO CrctObjAccountVO=new  CrctObjAccountVO();
			webResp.setData(crctObjAccountService.getList(CrctObjAccountVO));
			webResp.setTotalCount(15);
			return webResp;
		}
		 //增加
		@RequestMapping("/save_CrctObjAccount")
		@ResponseBody
		public Resp save_CrctObjAccount( Integer pageIndex,
				Integer pageSize,HttpServletRequest request) {
			WebResp 	webResp = new WebResp();
		   CrctObjAccountVO CrctObjAccountVO=new  CrctObjAccountVO();
		   
		  
		   CrctObjAccountVO.setModify_date(new Date());
		   crctObjAccountService.save(CrctObjAccountVO);
			return webResp;
		}
		 //增加
		@RequestMapping("/save_CrctObjAccount_type")
		@ResponseBody
		public Resp save_CrctObjAccount_type(
				String username,
				String password,
				String name,
				String card_id,
				HttpServletRequest request) {
			  DataResp dataResp = new DataResp();
			  com.zmks.jkgj.core.util.StringUtils stringUtils=new com.zmks.jkgj.core.util.StringUtils();
			try {
				 //验证用户名
				   if(StringUtils.isBlank(username)||!stringUtils.isZhEnNum(username)
						||username.length()<4||username.length()>20)
						{
							dataResp.setRet(1);
							dataResp.setErrcode(0);
							dataResp.setMsg("请正确输入账号(4~20位中英文字符或数字)!");
							return dataResp;
						}
						//验证密码
				  if(StringUtils.isBlank(password)||!stringUtils.isZhEnNum(password)
						||password.length()<8||password.length()>20)
						{  
							 dataResp.setRet(1);
						     dataResp.setErrcode(0);
						     dataResp.setMsg("请正确输入密码(8~20位中英文字符或数字)!");
						     return dataResp;
						}
					//验证姓名
				  if(StringUtils.isBlank(name)||!stringUtils.isZhEnNum(name)
						||name.length()<2)
						{  
							 dataResp.setRet(1);
						     dataResp.setErrcode(0);
						     dataResp.setMsg("请正确输入姓名!");
						     return dataResp;
						}
					//验证身份证
				  IdcardValidator iv = new IdcardValidator();
				  if(!iv.isIdcard(card_id))
						 
						{   dataResp.setRet(1);
						     dataResp.setErrcode(0);
						     dataResp.setMsg("身份号)!");
						     return dataResp;
						}
						// 验证用户是否存在
				  CrctObjAccountVO crctObjAccountVO_yan=new  CrctObjAccountVO();
				  crctObjAccountVO_yan.setUsername(username);
			 	if (null != crctObjAccountService.getUniqueResult(crctObjAccountVO_yan)) {
							dataResp.setRet(1);
							dataResp.setErrcode(0);
							dataResp.setMsg("该账号已被注册,请您更换用户名!");
							return dataResp;
						}
				
			CrctObjTeacherVO crctObjTeacherVO=new  CrctObjTeacherVO();
			crctObjTeacherVO.setName(name);
			crctObjTeacherVO.setCard_id(card_id);
			 CrctObjTeacherVO   crctObjTeacherVO2=  crctObjTeacherService.getUniqueResult(crctObjTeacherVO);
			 if(crctObjTeacherVO2==null)
			 {
					dataResp.setRet(1);
					dataResp.setErrcode(0);
					dataResp.setMsg("无此教师，请正确输入身份证号和姓名!");
					return dataResp;
			 }
			 
			 if(crctObjTeacherVO2!=null){
             CrctObjAccountVO crctObjAccountVO=new  CrctObjAccountVO();
             crctObjAccountVO.setOid(crctObjAccountVO.getId());
             crctObjAccountVO.setUsername(username);
             System.out.println("加密："+password);
             crctObjAccountVO.setPassword(Md5.encode(password));
             crctObjAccountVO.setCreate_date(new Date());
             if(crctObjTeacherVO2.getJob().equals("admin"))
             {
            	 crctObjAccountVO.setPermission("1");
             }
             CrctObjAccountVO crctObjAccountVO2=   crctObjAccountService.save(crctObjAccountVO);
             crctObjTeacherVO2.setAccount_oid(crctObjAccountVO2.getOid());
             crctObjTeacherService.save(crctObjTeacherVO2);
			  }
			} catch (Exception e) {
				e.printStackTrace();
				dataResp.setRet(1);
				dataResp.setErrcode(0);
				dataResp.setMsg("注册失败！请您稍后在进行注册！");
				return dataResp;
			}
		    return dataResp;
		
		}
		 //删除
		@RequestMapping("/remove_CrctObjAccount")
		@ResponseBody
		public Resp remove_CrctObjAccount( Integer pageIndex,
				Integer pageSize,HttpServletRequest request) {
			WebResp 	webResp = new WebResp();
			CrctObjAccountVO CrctObjAccountVO=new  CrctObjAccountVO();
			crctObjAccountService.remove(CrctObjAccountVO);
			return webResp;
		}
		 //登录
		@RequestMapping("/login_CrctObjAccount")
		@ResponseBody
		public Resp  login_CrctObjAccount( String username,
				String password,HttpServletRequest request) {
			WebResp 	webResp = new WebResp();
			try {
				  com.zmks.jkgj.core.util.StringUtils stringUtils=new com.zmks.jkgj.core.util.StringUtils();
					
				 //验证用户名
				   if(StringUtils.isBlank(username)||!stringUtils.isZhEnNum(username)
						||username.length()<4||username.length()>20)
						{
					   webResp.setRet(1);
					   webResp.setErrcode(0);
					   webResp.setMsg("请正确输入账号(4~20位中英文字符或数字)!");
							return webResp;
						}
						//验证密码
				  if(StringUtils.isBlank(password)||!stringUtils.isZhEnNum(password)
						||password.length()<8||password.length()>20)
						{  
					         webResp.setRet(1);
							 webResp.setErrcode(0);
							 webResp.setMsg("请正确输入密码(8~20位中英文字符或数字)!");
						     return webResp;
						}
			  CrctObjAccountVO crctObjAccountVO=new  CrctObjAccountVO();
			   crctObjAccountVO.setUsername(username);
			
			
			 CrctObjAccountVO  crctObjAccountVO_obj=	crctObjAccountService.getUniqueResult(crctObjAccountVO);
			 if(crctObjAccountVO_obj==null)
				{
					webResp.setRet(1);
					webResp.setMsg("无此账号！");
					return webResp;
				}
			 boolean flag = false;
			  System.out.println("加密："+Md5.encode(password));
			 if (crctObjAccountVO_obj.getPassword().equals(Md5.encode(password))) {
				 flag = true;
					HttpSession session = request.getSession(); 
					
					session.setAttribute("username", crctObjAccountVO_obj.getUsername()); 
					session.setAttribute("permission", crctObjAccountVO_obj.getPermission()); 
					System.out.println("登录sessionID:"+session.getId());
					HashMap map=new HashMap();
				    map.put("sessionid", session.getId());
				    String permission=crctObjAccountVO_obj.getPermission();
					map.put("permission",permission);
					 Session s=new Session();
					//超级管理员
					//学校管理
					 CrctObjTeacherVO  c=new CrctObjTeacherVO();
					 if(permission.equals("1")){
						c.setAccount_oid(crctObjAccountVO_obj.getOid());
						CrctObjTeacherVO cv=	 crctObjTeacherService.getUniqueResult(c);
						s.setSchool_oid(cv.getSchool_oid().toString()); 
						session.setAttribute("session", s);
					 }
					 
					//班主任管理
                    if(permission.equals("")){
						 
					 }
					//家长管理
                    if(permission.equals("")){
						 
					 }
					//学生管理
                    if(permission.equals("")){
						 
					 }
					
					webResp.setData(map);
				
					return webResp;
				 
			}else
			{
				webResp.setRet(1);
				webResp.setMsg("密码不正确");
				return webResp;
		
			}
			
			
			
//			if(username.equals("xuexiao")&&password.equals("xuexiao"))
//			{ 
//				
//				HttpSession session = request.getSession(); 
//				session.setAttribute("username", "admin"); 
//				session.setAttribute("type", "xuexiao"); 
//				System.out.println("登录sessionID:"+session.getId());
//				HashMap map=new HashMap();
//				map.put("JSESSIONID", session.getId());
//				map.put("type", "xuexiao");
//				webResp.setData(map);
//			
//				return webResp;
//			}
//			if(username.equals("banzhuren")&&password.equals("banzhuren"))
//			{ 
//				HttpSession session = request.getSession(); 
//				session.setAttribute("username", "admin"); 
//				session.setAttribute("type", "banzhuren"); 
//				System.out.println("登录sessionID:"+session.getId());
//				HashMap map=new HashMap();
//				map.put("JSESSIONID", session.getId());
//				map.put("type", "banzhuren");
//				webResp.setData(map);
//			
//				return webResp;
//			}
//			if(username.equals("admin")&&password.equals("admin"))
//			{ 
//				HttpSession session = request.getSession(); 
//				session.setAttribute("username", "admin"); 
//				session.setAttribute("type", "chaoji"); 
//				System.out.println("登录sessionID:"+session.getId());
//				webResp.setData("chaoji");
//				HashMap map=new HashMap();
//				map.put("sessionid", session.getId());
//				map.put("type", "chaoji");
//				webResp.setData(map);
//				return webResp;
//			}
//			if(username.equals("jiazhang")&&password.equals("jiazhang"))
//			{ 
//				
//				HttpSession session = request.getSession(); 
//				session.setAttribute("username", "jiazhang"); 
//				session.setAttribute("type", "jiazhang"); 
//				System.out.println("登录sessionID:"+session.getId());
//				HashMap map=new HashMap();
//				map.put("JSESSIONID", session.getId());
//				map.put("type", "jiazhang");
//				webResp.setData(map);
//			
//				return webResp;
//			}
//			if(username.equals("xuesheng")&&password.equals("xuesheng"))
//			{ 
//				
//				HttpSession session = request.getSession(); 
//				session.setAttribute("username", "xuesheng"); 
//				session.setAttribute("type", "xuesheng"); 
//				System.out.println("登录sessionID:"+session.getId());
//				HashMap map=new HashMap();
//				map.put("JSESSIONID", session.getId());
//				map.put("type", "xuesheng");
//				webResp.setData(map);
//			
//				return webResp;
//			}
			
			} catch (Exception e) {
				e.printStackTrace();
				webResp.setRet(1);
				webResp.setErrcode(0);
				webResp.setMsg("登录失败！请您稍后在进行登录！");
				return webResp;
			}
		}
		
		 //退出登录
		@RequestMapping("/loginOut_CrctObjAccount")
		@ResponseBody
		public Resp  loginOut_CrctObjAccount(HttpServletRequest request) {
		
			WebResp 	webResp = new WebResp();
				HttpSession session = request.getSession(); 
				System.out.println("当前sessionID:"+session.getId());
				System.out.println("当前session:"+session.getAttribute("username"));
				session.invalidate();
	   return webResp;
		}
		
		 //登录验证
		@RequestMapping("/islogin_CrctObjAccount")
		@ResponseBody
		public Resp  islogin_CrctObjAccount(HttpServletRequest request) {
		
			WebResp 	webResp = new WebResp();
			HttpSession session = request.getSession(); 
			  System.out.println("sesss"+session.getId());
			  System.out.println("username"+session.getAttribute("username"));
			   System.out.println("permission"+session.getAttribute("permission"));
		      HashMap map=new HashMap();
			map.put("username", session.getAttribute("username"));
			map.put("permission", session.getAttribute("permission"));
			webResp.setData(map);
			 return webResp;
		}
	  
	
	
	

}
